package fibonacci;

public class Way3 {
	
	public static void main(String[] args) {
		final int num = 20;
		Way3 way3 = new Way3();
		//结果为：6765，一共执行了19次
		System.out.println("结果为："+way3.fab(num)+"，一共执行了"+way3.count+"次");
	}
	
	private int count = 0;
	
	public int fab(int num) {
		if(num<=0) {
			count++;
			return 0;
		}
		if(num==1) {
			count++;
			return 1;
		}
		int one = 0,another = 1,sum = 0;
		for(int i=2;i<=num;i++) {
			count++;
			sum = one + another;
			one = another;
			another = sum;
		}
		return sum;
	}

}
